﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace 花店管理系统.Customer_Services
{
    public partial class Servers : Form
    {
        private string currentUserID;
        public Servers()
        {
            InitializeComponent();
            this.currentUserID = Login.t_id;
            LoadConversation();
        }
        private void LoadConversation()
        {
            using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["花店管理系统.Properties.Settings.huadianConnectionString"].ConnectionString))
            {
                richTextBox1.Clear();
                connection.Open();
                string query = "SELECT Sender, MessageContent FROM Messages WHERE UserID=@UserID ORDER BY SendTime";
                using (SqlCommand command = new SqlCommand(query, connection))
                {
                    command.Parameters.AddWithValue("@UserID", currentUserID);
                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            string sender = reader["Sender"].ToString();
                            string messageContent = reader["MessageContent"].ToString();

                            richTextBox1.SelectionStart = richTextBox1.TextLength;
                            richTextBox1.SelectionLength = 0;

                            if (sender == Login.username)
                            {
                                // Align to the right for messages from the current user
                                richTextBox1.SelectionAlignment = HorizontalAlignment.Right;
                            }
                            else
                            {
                                // Align to the left for messages from others
                                richTextBox1.SelectionAlignment = HorizontalAlignment.Left;
                            }

                            richTextBox1.AppendText(sender + ": " + messageContent + Environment.NewLine);
                        }
                    }
                }
            }
        }

      
        private void SaveMessageToDatabase(string sender, string messageContent)
        {
            using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["花店管理系统.Properties.Settings.huadianConnectionString"].ConnectionString))
            {
                connection.Open();
                string query = "INSERT INTO Messages (Sender, MessageContent, SendTime,UserID) VALUES (@Sender, @MessageContent, @SendTime,@UserID)";
                using (SqlCommand command = new SqlCommand(query, connection))
                {
                    command.Parameters.AddWithValue("@Sender", sender);
                    command.Parameters.AddWithValue("@MessageContent", messageContent);
                    command.Parameters.AddWithValue("@SendTime", DateTime.Now);
                    command.Parameters.AddWithValue("@UserID", currentUserID);
                    command.ExecuteNonQuery();
                 }
            }
        }

        private void btnSend1_Click(object sender, EventArgs e)
        {
            //设置文本内容不为空字符以及不能为空格
            if (txtUserMessage1.Text == "")//移除所有空白前导字符以及尾部字符
            {
                MessageBox.Show("发送内容不能为空,请至少包含一个字符!");
            }
            else
            {
                string userMessage = txtUserMessage1.Text;
                string s = Login.username;
                SaveMessageToDatabase(s, userMessage);
                //LoadConversation();
                richTextBox1.SelectionStart = richTextBox1.TextLength;
                richTextBox1.SelectionLength = 0;
                richTextBox1.SelectionAlignment = HorizontalAlignment.Right;
                richTextBox1.AppendText(s + ":" + userMessage + Environment.NewLine);
                txtUserMessage1.Clear();
            }
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}


